Introduction

In this reading, you will recap some key concepts explained previously in this course, related to fetching data from a remote server and storing it in Core Data. In the case of this Little Lemon Restaurant project, you retrieve a food menu from a remote server and the result is formatted in JSON.

Scenario

Core Data works by creating entities that store the real data, like an entity called Dish , for the Little Lemon Restaurant app, which stores names, sizes, and prices.

Secondly, you have relationships. Relationships link entities, for example, if you need to store information about which dishes were consumed by which customers.

As you complete the next set of exercises, refer to the following information about Core Data, entities and relationships from the Working with Data in iOS course:

Creating structs for your data

As soon as you get the JSON from the server, you must convert it to a suitable Swift format.

To do that you first create structs to represent the data, like the following ones:

struct JSONMenu: Codable { 
    let menu: [MenuItem]     
} 


struct MenuItem: Codable, Identifiable { 
    var id = UUID() 
    let title: String 
    let price: String 
    let description: String 
    let image: String 
} 

Then, retrieve the data and decode it using the defined structs.

      let (data, _) = try await urlSession.data(from: url)  
      let fullMenu = try JSONDecoder().decode(JSONMenu.self, from: data) 
      menuItems = fullMenu.menu 

The result, which is menuItems in this code, is an array of MenuItems .

For more information, please review the reading Exemplar: Little Lemon Menu App in Module 4, Lesson 1 of the course Working with Data in iOS .

Converting the formats

The next phase will be to convert these items into the format used by the entity Dish . Note that MenuItems has four properties: name , price , description , and image . These are the same properties as Dish .

To do that, you add a function to the Dish extension class to do this:

static func createDishesFrom(menuItems:[MenuItem], 
                                 _ context:NSManagedObjectContext) { 
        for menuItem in menuItems { 
            guard let _ = exists(name: menuItem.title, context) else { 
                continue 
            } 
            let oneDish = Dish(context: context) 
            oneDish.name = menuItem.title 
            if let price = Float(menuItem.price) { 
                oneDish.price = price 
            } 
            oneDish.description = menuItem.description 
            oneDish.image = menuItem.image 
        } 
    } 

This code takes an array of MenuItems and creates dishes in the Dish entity populating them with properties from the MenuItems .

For more information, please review the reading Displaying the food menu via Core Data in Module 4, Lesson 1 of the course Working with Data in iOS .

Conclusion

In this reading, you’ve recapped the key aspects of fetching data from a remote server, converting everything to a suitable Swift format, and then storing it in Core Data.